package wortubung.inflection;

import static wortubung.util.CommonUtils.prefix;
import wortubung.core.LANG;

public class DerivedVerbConjugation implements VerbConjugation {

	private String prefix;
	private String meaning;
	private String[] usage;
	private VerbConjugation base;

	public DerivedVerbConjugation(String prefix, VerbConjugation base) {
		this.prefix = prefix;
		this.base = base;
	}

	@Override public String getVerb() { return prefix + base.getVerb(); }
	@Override public boolean isSuffixOnly() { return false; }
	@Override public boolean isRootSuffixOnly() { return base != null && base.isSuffixOnly(); }
	@Override public String getPrefix() { return prefix; }
	@Override public String getDerivedFrom() { return base.getVerb(); }
	@Override public Integer getRefIndex1() { return base.getRefIndex1(); }
	@Override public Integer getRefIndex2() { return base.getRefIndex2(); }
	@Override public boolean hasAlternative() { return base.hasAlternative(); }
	@Override public String getMeaning() { return meaning; }
	@Override public String[] getUsage() { return usage; }

	@Override public ConjugationResult getConjugationResult(LANG langForLatinizing) {
		String pref = prefix;
		return base.getConjugationResult(langForLatinizing).forPrefix(pref, getPrefixProcessor());
	}

	protected PrefixProcessor getPrefixProcessor() {
		final PrefixProcessor pp = new PrefixProcessor();
		return pp;
	}

	public void setUsage(String[] usage) { this.usage = usage; }
	public void setMeaning(String meaning) { this.meaning = meaning; }

	public static class PrefixProcessor {

		protected String toPresentParticiple(String prefix, ConjugationSet base) {
			return prefix(prefix, base.getPresentParticiple());
		}

		protected String toPastParticiple(String prefix, ConjugationSet base) {
			return prefix(prefix, base.getPastParticiple());
		}

		protected String toConjugation(String prefix, String baseConj, CONJUGATION conjType) {
			return prefix(prefix, baseConj);
		}

		public final ConjugationSet process(String prefix, ConjugationSet base) {
			ConjugationSet newCS = new ConjugationSet(null, toPresentParticiple(prefix, base), toPastParticiple(prefix, base));
			for (CONJUGATION c : CONJUGATION.values()) {
				String[] sa = base.get(c);
				if (sa == null)
					continue;
				String[] newSa = new String[sa.length];
				for (int i=sa.length-1; i>=0; --i)
					newSa[i] = toConjugation(prefix, sa[i], c);
				newCS.add(c, newSa);
			}
			return newCS;
		}
	}
}
